/*
 * $Id: Config.java,v 1.2 2003/05/21 23:09:15 znamenacek Exp $
 *
 * Copyright (c) Tomas Znamenacek
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

package net.sf.vofce.project;

import net.sf.vofce.util.NiceProperties;
import org.apache.log4j.Category;

import java.awt.Color;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;

/**
 * Configuration system.
 * Simple singleton class that extends <code>NiceProperties</code> and
 * provides a key/value configuration mechanism.
 *
 * @author Tomas Znamenacek
 */
public class Config extends NiceProperties {

	/** Default function graph colors. */
	protected static final Color[] DEFAULT_COLORS = {
		Color.red,
		Color.blue,
		Color.green,
		Color.orange,
	};
	
	/** Logger instance. */
	protected static Category logger = Category.getRoot();

	/** Config system instance. */
	protected static Config instance = new Config();

	/** Returns the instance of the configuration system. */
	public static Config getInstance() {

		return instance;
	}
	
	/** Creates instance and loads properties from file. */
	private Config() {
		
		super();
		loadFromFile();
	}

	/** 
	 * Returns the configuration file location that depends on the OS
	 * we're running.
	 */
	protected File getFileName() throws Exception {

		// TODO: maybe we want to get some better location,
		// this sucks on Win9x, there's no home directory...
		
		String home = System.getProperty("user.home");
		String sep = System.getProperty("file.separator");
		
		File configDir = new File(home + sep + ".vofce");

		if (! configDir.exists()) {

			if (! configDir.mkdir()) {
				logger.error("Can't create directory: " + configDir.getPath());
				throw new Exception();
			}
		}
	
		if (configDir.isFile()) {
			logger.warn("Config: Can't create config directory, file exists: " + configDir.getPath());
			throw new Exception();
		}

		// No we know ~/.vofce exists and is a directory
	
		File configFile = new File(configDir.getPath() + sep + "vofcerc");

		try {
			configFile.createNewFile();
		} catch (Exception e) {
				logger.error("Config: Can't create file: " + configFile.getPath());
			throw new Exception();
		}

		try {
			if (! configFile.canWrite()) {
				logger.error("Config: File not writable: " + configFile.getPath());
				throw new Exception();
			}
		} catch (Exception e) {
			logger.error("Config: File not writable: " + configFile.getPath());
			throw new Exception();
		}
	
		// We've got a ~/.vofce/vofcerc and it's writable
		return configFile;
	}
	
	/** Loads properties from file. */
	public void loadFromFile() {

		try {
			File file = getFileName();
			FileInputStream is = new FileInputStream(file);
			load(is);
			is.close();
		} catch (Exception e) {

			logger.warn("Config: could not load the configuration.");
		}
	}

	/** Saves properties to file. */
	public void saveToFile() {

		try {
			File file = getFileName();
			FileOutputStream os = new FileOutputStream(file);
			store(os, Project.SAVE_STAMP);
			os.close();
		} catch (Exception e) {

			logger.warn("Config: could not save the configuration.");
		}
	}
	
	/** Returns color for given function. */
	public Color getFunctionColor(int functionIndex) {
	
		return getColor("function" + functionIndex + ".color", DEFAULT_COLORS[functionIndex % DEFAULT_COLORS.length]);
	}
	
	/** Sets color for given function. */
	public void setFunctionColor(int functionIndex, Color color) {
	
		String key = "function" + functionIndex + ".color";
		setColor(key, color);
		firePropertyChanged(key);
	}
}
